include(AwsTestHarness)
enable_testing()

option(BYO_CRYPTO "Don't build a tls implementation or link against a crypto interface. This feature is only for unix builds currently." OFF)

if(BYO_CRYPTO)
    set(ENABLE_NET_TESTS OFF)

    add_test_case(test_s3_client_byo_crypto_no_options)
    add_test_case(test_s3_client_byo_crypto_with_options)
endif()

file(GLOB TEST_SRC "*.c")
file(GLOB TEST_HDRS "*.h")
file(GLOB TESTS ${TEST_HDRS} ${TEST_SRC})

add_test_case(test_s3_library_init_cleanup_init_cleanup)
add_test_case(test_s3_copy_http_message)
add_test_case(test_s3_message_util_assign_body)
add_test_case(test_s3_ranged_get_object_message_new)
add_test_case(test_s3_set_multipart_request_path)
add_test_case(test_s3_create_multipart_upload_message_new)
add_test_case(test_s3_upload_part_message_new)
add_test_case(test_s3_upload_part_message_fail)
add_test_case(test_s3_complete_multipart_message_new)
add_test_case(test_s3_abort_multipart_upload_message_new)

add_net_test_case(test_s3_client_create_destroy)
add_net_test_case(test_s3_client_create_error)
add_net_test_case(test_s3_client_monitoring_options_override)
add_net_test_case(test_s3_client_proxy_ev_settings_override)
add_net_test_case(test_s3_client_tcp_keep_alive_options_override)
add_net_test_case(test_s3_client_max_active_connections_override)
add_test_case(test_s3_client_get_max_active_connections)
add_test_case(test_s3_request_create_destroy)
add_test_case(test_s3_client_queue_requests)
add_test_case(test_s3_meta_request_body_streaming)
add_test_case(test_s3_update_meta_requests_trigger_prepare)
add_test_case(test_s3_client_update_connections_finish_result)

add_net_test_case(test_s3_client_exceed_retries)
add_net_test_case(test_s3_client_acquire_connection_fail)
add_net_test_case(test_s3_meta_request_fail_prepare_request)
add_net_test_case(test_s3_meta_request_sign_request_fail)
add_net_test_case(test_s3_meta_request_send_request_finish_fail)
add_net_test_case(test_s3_auto_range_put_missing_upload_id)

add_net_test_case(test_s3_cancel_mpu_create_not_sent)
add_net_test_case(test_s3_cancel_mpu_create_completed)
add_net_test_case(test_s3_cancel_mpu_one_part_completed)
add_net_test_case(test_s3_cancel_mpu_one_part_completed_async)
add_net_test_case(test_s3_cancel_mpu_all_parts_completed)
add_net_test_case(test_s3_cancel_mpu_cancellable_requests)
add_net_test_case(test_s3_pause_mpu_cancellable_requests)
add_net_test_case(test_s3_cancel_mpd_nothing_sent)
add_net_test_case(test_s3_cancel_mpd_one_part_sent)
add_net_test_case(test_s3_cancel_mpd_one_part_completed)
add_net_test_case(test_s3_cancel_mpd_two_parts_completed)
add_net_test_case(test_s3_cancel_mpd_head_object_sent)
add_net_test_case(test_s3_cancel_mpd_head_object_completed)
add_net_test_case(test_s3_cancel_mpd_empty_object_get_with_part_number_1_sent)
add_net_test_case(test_s3_cancel_mpd_empty_object_get_with_part_number_1_completed)
add_net_test_case(test_s3_cancel_mpd_pending_streaming)
add_net_test_case(test_s3_cancel_prepare)

add_net_test_case(test_s3_get_object_tls_disabled)
add_net_test_case(test_s3_get_object_tls_enabled)
add_net_test_case(test_s3_get_object_tls_default)
add_net_test_case(test_s3_get_object_less_than_part_size)
add_net_test_case(test_s3_get_object_file_path)
add_net_test_case(test_s3_get_object_file_path_create_new)
add_net_test_case(test_s3_get_object_file_path_append)
add_net_test_case(test_s3_get_object_file_path_to_position)
add_net_test_case(test_s3_get_object_empty_object)
add_net_test_case(test_s3_get_object_multiple)
add_net_test_case(test_s3_get_object_multiple_serial)
add_net_test_case(test_s3_get_object_sse_kms)
add_net_test_case(test_s3_get_object_sse_aes256)
add_net_test_case(test_s3_get_object_looks_like_async_error_xml)
add_net_test_case(test_s3_default_get_object_looks_like_async_error_xml)
add_net_test_case(test_s3_get_object_backpressure_small_increments)
add_net_test_case(test_s3_get_object_backpressure_big_increments)
add_net_test_case(test_s3_get_object_backpressure_initial_size_zero)
add_net_test_case(test_s3_get_object_backpressure_small_increments_recv_filepath)
add_net_test_case(test_s3_get_object_backpressure_big_increments_recv_filepath)
add_net_test_case(test_s3_get_object_backpressure_initial_size_zero_recv_filepath)
add_net_test_case(test_s3_get_object_part)
add_net_test_case(test_s3_no_signing)
add_net_test_case(test_s3_signing_override)
add_net_test_case(test_s3_put_object_tls_disabled)
add_net_test_case(test_s3_put_object_tls_enabled)
add_net_test_case(test_s3_put_object_tls_default)
add_net_test_case(test_s3_multipart_put_object_with_acl)
add_net_test_case(test_s3_put_object_multiple)
add_net_test_case(test_s3_put_object_multiple_with_filepath)
add_net_test_case(test_s3_put_object_less_than_part_size)
add_net_test_case(test_s3_put_object_less_than_part_size_with_content_encoding)
add_net_test_case(test_s3_put_object_mpu_with_content_encoding)
add_net_test_case(test_s3_put_object_multipart_threshold)
add_net_test_case(test_s3_put_object_multipart_threshold_less_than_part_size)
add_net_test_case(test_s3_put_object_empty_object)
add_net_test_case(test_s3_put_object_with_part_remainder)
add_net_test_case(test_s3_put_object_sse_kms)
add_net_test_case(test_s3_put_object_sse_kms_multipart)
add_net_test_case(test_s3_put_object_sse_aes256)
add_net_test_case(test_s3_put_object_sse_aes256_multipart)
add_net_test_case(test_s3_put_object_sse_c_aes256_multipart)
add_net_test_case(test_s3_put_object_sse_c_aes256_multipart_with_checksum)
add_net_test_case(test_s3_put_object_singlepart_no_content_md5_enabled)
add_net_test_case(test_s3_put_object_singlepart_no_content_md5_disabled)
add_net_test_case(test_s3_put_object_singlepart_correct_content_md5_enabled)
add_net_test_case(test_s3_put_object_singlepart_correct_content_md5_disabled)
add_net_test_case(test_s3_put_object_singlepart_incorrect_content_md5_enabled)
add_net_test_case(test_s3_put_object_singlepart_incorrect_content_md5_disabled)
add_net_test_case(test_s3_put_object_multipart_no_content_md5_enabled)
add_net_test_case(test_s3_put_object_multipart_no_content_md5_disabled)
add_net_test_case(test_s3_put_object_multipart_correct_content_md5_enabled)
add_net_test_case(test_s3_put_object_multipart_correct_content_md5_disabled)
add_net_test_case(test_s3_put_object_multipart_incorrect_content_md5_enabled)
add_net_test_case(test_s3_put_object_multipart_incorrect_content_md5_disabled)
add_net_test_case(test_s3_upload_part_message_with_content_md5)
add_net_test_case(test_s3_upload_part_message_without_content_md5)
add_net_test_case(test_s3_create_multipart_upload_message_with_content_md5)
add_net_test_case(test_s3_complete_multipart_message_with_content_md5)
add_net_test_case(test_s3_put_object_double_slashes)
add_net_test_case(test_s3_put_object_no_content_length)
add_net_test_case(test_s3_put_object_single_part_no_content_length)
add_net_test_case(test_s3_put_object_zero_size_no_content_length)
add_net_test_case(test_s3_put_large_object_no_content_length_with_checksum)
add_net_test_case(test_s3_put_object_no_content_length_multiple)
add_net_test_case(test_s3_put_object_async_singlepart)
add_net_test_case(test_s3_put_object_async_multipart)
add_net_test_case(test_s3_put_object_async_read_completes_synchronously)
add_net_test_case(test_s3_put_object_small_reads)
add_net_test_case(test_s3_put_object_async_small_reads)
add_net_test_case(test_s3_put_object_async_no_content_length_partial_part)
add_net_test_case(test_s3_put_object_async_no_content_length_1part)
add_net_test_case(test_s3_put_object_async_no_content_length_empty_part2)
add_net_test_case(test_s3_put_object_async_no_content_length_2parts)
add_net_test_case(test_s3_put_object_async_fail_reading)
add_net_test_case(test_s3_many_async_uploads_without_data)
add_net_test_case(test_s3_download_empty_file_with_checksum)
add_net_test_case(test_s3_download_single_part_file_with_checksum)
add_net_test_case(test_s3_download_multipart_file_with_checksum)
add_net_test_case(test_s3_asyncwrite_empty_file)
add_net_test_case(test_s3_asyncwrite_small_file_1_write)
add_net_test_case(test_s3_asyncwrite_small_file_1_write_then_eof)
add_net_test_case(test_s3_asyncwrite_small_file_many_writes)
add_net_test_case(test_s3_asyncwrite_1_part)
add_net_test_case(test_s3_asyncwrite_1_part_many_writes)
add_net_test_case(test_s3_asyncwrite_1_part_then_eof)
add_net_test_case(test_s3_asyncwrite_2_parts_2_partsize_writes)
add_net_test_case(test_s3_asyncwrite_2_parts_1_write)
add_net_test_case(test_s3_asyncwrite_2_parts_first_write_over_partsize)
add_net_test_case(test_s3_asyncwrite_2_parts_first_write_under_partsize)
add_net_test_case(test_s3_asyncwrite_tolerate_empty_writes)
add_net_test_case(test_s3_asyncwrite_write_from_future_callback)
add_net_test_case(test_s3_asyncwrite_fails_if_request_has_completed)
add_net_test_case(test_s3_asyncwrite_fails_if_write_after_eof)
add_net_test_case(test_s3_asyncwrite_fails_if_writes_overlap)
add_net_test_case(test_s3_asyncwrite_cancel_sends_abort)

if(ENABLE_MRAP_TESTS)
    add_net_test_case(test_s3_get_object_less_than_part_size_mrap)
    add_net_test_case(test_s3_get_object_multipart_mrap)
    add_net_test_case(test_s3_put_object_less_than_part_size_mrap)
    add_net_test_case(test_s3_put_object_multipart_mrap)
endif()

add_net_test_case(test_s3_round_trip)
add_net_test_case(test_s3_round_trip_default_get)
add_net_test_case(test_s3_round_trip_multipart_get_fc)
add_net_test_case(test_s3_round_trip_default_get_fc)
add_net_test_case(test_s3_round_trip_mpu_multipart_get_fc)
add_net_test_case(test_s3_round_trip_mpu_multipart_get_with_list_algorithm_fc)
add_net_test_case(test_s3_round_trip_mpu_default_get_fc)
add_net_test_case(test_s3_round_trip_with_filepath)
add_net_test_case(test_s3_round_trip_mpu_with_filepath)
add_net_test_case(test_s3_round_trip_with_filepath_no_content_length)
add_net_test_case(test_s3_round_trip_mpu_with_filepath_no_content_length)
add_net_test_case(test_s3_chunked_then_unchunked)

add_net_test_case(test_s3_cancel_mpu_one_part_completed_fc)
add_net_test_case(test_s3_cancel_mpd_one_part_completed_fc)

add_net_test_case(test_s3_meta_request_default)
add_net_test_case(test_s3_put_object_fail_headers_callback)
add_net_test_case(test_s3_put_object_fail_body_callback)
add_net_test_case(test_s3_get_object_fail_headers_callback)
add_net_test_case(test_s3_get_object_fail_body_callback)
add_net_test_case(test_s3_default_fail_headers_callback)
add_net_test_case(test_s3_default_invoke_headers_callback_on_error)
add_net_test_case(test_s3_default_invoke_headers_callback_cancels_on_error)
add_net_test_case(test_s3_get_object_invoke_headers_callback_on_error)
add_net_test_case(test_s3_put_object_invoke_headers_callback_on_error)
add_net_test_case(test_s3_put_object_invoke_headers_callback_on_error_with_user_cancellation)
add_net_test_case(test_s3_default_fail_body_callback)
add_net_test_case(test_s3_default_fail_operation_name)
add_net_test_case(test_s3_error_missing_file)
add_net_test_case(test_s3_existing_host_entry)
add_net_test_case(test_s3_put_fail_object_invalid_request)
add_net_test_case(test_s3_put_fail_object_invalid_send_filepath)
add_net_test_case(test_s3_put_fail_object_bad_parallel_read_stream)
add_net_test_case(test_s3_put_fail_object_inputstream_fail_reading)
add_net_test_case(test_s3_put_fail_object_inputstream_mismatch_content_length)
add_net_test_case(test_s3_put_single_part_fail_object_inputstream_fail_reading)
add_net_test_case(test_s3_put_single_part_fail_object_inputstream_mismatch_content_length)
add_net_test_case(test_s3_put_object_clamp_part_size)
add_net_test_case(test_s3_auto_ranged_get_sending_user_agent)
add_net_test_case(test_s3_auto_ranged_put_sending_user_agent)
add_net_test_case(test_s3_default_sending_meta_request_user_agent)
add_net_test_case(test_s3_range_requests)
add_net_test_case(test_s3_not_satisfiable_range)
add_net_test_case(test_s3_invalid_start_range_greator_than_end_range)
add_net_test_case(test_s3_invalid_empty_file_with_range)

add_net_test_case(test_s3_bad_endpoint)
add_net_test_case(test_s3_different_endpoints)

add_test_case(test_s3_request_type_operation_name)
add_test_case(test_s3_request_type_from_operation_name)
add_test_case(test_s3_replace_quote_entities)
add_test_case(test_s3_strip_quotes)
add_test_case(test_s3_parse_request_range_header)
add_test_case(test_s3_parse_content_range_response_header)
add_test_case(test_s3_parse_content_length_response_header)
add_test_case(test_s3_get_num_parts_and_get_part_range)
add_test_case(test_s3_mpu_get_part_size_and_num_parts)
add_test_case(test_s3_aws_xml_get_body_at_path)
add_test_case(test_add_user_agent_header)

add_test_case(test_get_existing_platform_info)
add_test_case(test_get_nonexistent_platform_info)
add_test_case(test_get_platforms_with_recommended_config)
add_net_test_case(load_platform_info_from_global_state_sanity_test)

add_net_test_case(sha1_nist_test_case_1)
add_net_test_case(sha1_nist_test_case_2)
add_net_test_case(sha1_nist_test_case_3)
add_net_test_case(sha1_nist_test_case_4)
add_net_test_case(sha1_nist_test_case_5)
add_net_test_case(sha1_nist_test_case_5_truncated)
add_net_test_case(sha1_nist_test_case_6)
add_net_test_case(sha1_test_invalid_buffer)
add_net_test_case(sha1_test_oneshot)
add_net_test_case(sha1_test_invalid_state)

add_net_test_case(sha256_nist_test_case_1)
add_net_test_case(sha256_nist_test_case_2)
add_net_test_case(sha256_nist_test_case_3)
add_net_test_case(sha256_nist_test_case_4)
add_net_test_case(sha256_nist_test_case_5)
add_net_test_case(sha256_nist_test_case_5_truncated)
add_net_test_case(sha256_nist_test_case_6)
add_net_test_case(sha256_test_invalid_buffer)
add_net_test_case(sha256_test_oneshot)
add_net_test_case(sha256_test_invalid_state)

add_test_case(crc32_nist_test_case_1)
add_test_case(crc32_nist_test_case_2)
add_test_case(crc32_nist_test_case_3)
add_test_case(crc32_nist_test_case_4)
add_test_case(crc32_nist_test_case_5)
add_test_case(crc32_nist_test_case_5_truncated)
add_test_case(crc32_nist_test_case_6)
add_test_case(crc32_test_invalid_buffer)
add_test_case(crc32_test_oneshot)
add_test_case(crc32_test_invalid_state)

add_test_case(crc32c_nist_test_case_1)
add_test_case(crc32c_nist_test_case_2)
add_test_case(crc32c_nist_test_case_3)
add_test_case(crc32c_nist_test_case_4)
add_test_case(crc32c_nist_test_case_5)
add_test_case(crc32c_nist_test_case_5_truncated)
add_test_case(crc32c_nist_test_case_6)
add_test_case(crc32c_test_invalid_buffer)
add_test_case(crc32c_test_oneshot)
add_test_case(crc32c_test_invalid_state)

add_net_test_case(verify_checksum_stream)
add_net_test_case(verify_chunk_stream)

add_net_test_case(test_s3_copy_small_object)
add_net_test_case(test_s3_copy_small_object_special_char)
add_net_test_case(test_s3_multipart_copy_large_object_special_char)
add_net_test_case(test_s3_multipart_copy_large_object)
add_net_test_case(test_s3_copy_object_invalid_source_key)
add_net_test_case(test_s3_copy_source_prefixed_by_slash)
add_net_test_case(test_s3_copy_source_prefixed_by_slash_multipart)
add_net_test_case(test_s3_put_pause_resume_happy_path)
add_net_test_case(test_s3_put_pause_resume_all_parts_done)
add_net_test_case(test_s3_put_pause_resume_invalid_resume_data)
add_net_test_case(test_s3_put_pause_resume_invalid_content_length)
add_net_test_case(test_s3_upload_review)
add_net_test_case(test_s3_upload_review_no_content_length)
add_net_test_case(test_s3_upload_review_rejection)
add_net_test_case(test_s3_upload_review_checksum_location_none)
add_net_test_case(test_s3_upload_review_checksum_location_none_async)
add_net_test_case(test_s3_upload_review_checksum_location_none_async_noop_part)

add_net_test_case(test_s3_list_bucket_init_mem_safety)
add_net_test_case(test_s3_list_bucket_init_mem_safety_optional_copies)
add_net_test_case(test_s3_list_bucket_valid)

# Tests against local mock server
if(ENABLE_MOCK_SERVER_TESTS)
    add_net_test_case(multipart_upload_mock_server)
    add_net_test_case(multipart_upload_with_network_interface_names_mock_server)
    add_net_test_case(multipart_upload_checksum_with_retry_mock_server)
    add_net_test_case(multipart_download_checksum_with_retry_mock_server)
    add_net_test_case(async_internal_error_from_complete_multipart_mock_server)
    add_net_test_case(async_access_denied_from_complete_multipart_mock_server)
    add_net_test_case(get_object_modified_mock_server)
    add_net_test_case(get_object_invalid_responses_mock_server)
    add_net_test_case(get_object_mismatch_checksum_responses_mock_server)
    add_net_test_case(get_object_throughput_failure_mock_server)
    add_net_test_case(get_object_long_error_mock_server)
    add_net_test_case(upload_part_invalid_response_mock_server)
    add_net_test_case(upload_part_async_invalid_response_mock_server)
    add_net_test_case(resume_first_part_not_completed_mock_server)
    add_net_test_case(resume_multi_page_list_parts_mock_server)
    add_net_test_case(resume_list_parts_failed_mock_server)
    add_net_test_case(resume_after_finished_mock_server)
    add_net_test_case(multipart_upload_proxy_mock_server)
    add_net_test_case(endpoint_override_mock_server)

    add_net_test_case(s3express_provider_sanity_mock_server)
    add_net_test_case(s3express_provider_get_credentials_mock_server)
    add_net_test_case(s3express_provider_get_credentials_multiple_mock_server)
    add_net_test_case(s3express_provider_get_credentials_cancel_mock_server)
    add_net_test_case(s3express_provider_get_credentials_cache_mock_server)
    add_net_test_case(s3express_provider_background_refresh_mock_server)
    add_net_test_case(s3express_provider_background_refresh_remove_inactive_creds_mock_server)
    add_net_test_case(s3express_provider_stress_mock_server)

    add_net_test_case(s3express_client_sanity_test_mock_server)
    add_net_test_case(s3express_client_sanity_override_test_mock_server)
    add_net_test_case(request_time_too_skewed_mock_server)
endif()

add_net_test_case(s3express_provider_long_running_session_refresh)

add_net_test_case(s3express_client_put_object)
add_net_test_case(s3express_client_put_object_multipart)
add_net_test_case(s3express_client_put_object_multipart_multiple)
add_net_test_case(s3express_client_put_object_long_running_session_refresh)
add_net_test_case(s3express_client_get_object)
add_net_test_case(s3express_client_get_object_multiple)
add_net_test_case(s3express_client_get_object_create_session_error)

add_net_test_case(meta_request_auto_ranged_get_new_error_handling)
add_net_test_case(meta_request_auto_ranged_put_new_error_handling)
add_net_test_case(bad_request_error_handling)
add_net_test_case(make_meta_request_error_handling)

if(AWS_ENABLE_S3_ENDPOINT_RESOLVER)
    add_test_case(test_s3_endpoint_resolver_resolve_endpoint)
    add_test_case(test_s3_endpoint_resolver_resolve_endpoint_fips)
    add_test_case(test_s3_endpoint_resolver_resolve_endpoint_force_path_style)
endif()

add_test_case(parallel_read_stream_from_file_sanity_test)
add_test_case(parallel_read_stream_from_large_file_test)

add_test_case(test_s3_buffer_pool_threaded_allocs_and_frees)
add_test_case(test_s3_buffer_pool_large_chunk_threaded_allocs_and_frees)
add_test_case(test_s3_buffer_pool_limits)
add_test_case(test_s3_buffer_pool_trim)
add_test_case(test_s3_buffer_pool_reservation_hold)
add_test_case(test_s3_buffer_pool_too_small)
add_net_test_case(test_s3_put_object_buffer_pool_trim)
add_test_case(test_s3_buffer_pool_forced_buffer)
add_test_case(test_s3_buffer_pool_forced_buffer_after_reservation_hold)
add_test_case(test_s3_buffer_pool_forced_buffer_wont_stop_reservations)

add_net_test_case(client_update_upload_part_timeout)
add_net_test_case(client_meta_request_override_part_size)
add_net_test_case(client_meta_request_override_multipart_upload_threshold)

set(TEST_BINARY_NAME ${PROJECT_NAME}-tests)
generate_test_driver(${TEST_BINARY_NAME})

if(AWS_ENABLE_S3_ENDPOINT_RESOLVER)
    target_compile_definitions(${PROJECT_NAME}-tests PRIVATE "-DAWS_ENABLE_S3_ENDPOINT_RESOLVER")
endif()
